<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="ID" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="基地" prop="基地">
        <el-select
          v-model="queryParams.基地"
          placeholder="请输入基地"
          clearable
          @keyup.enter.native="handleQuery"
          @change="handleSelectChange" > >
          <el-option
            v-for="item in option1"
            :key="item.value"
            :label="item.l"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车间" prop="车间">
        <el-select
          v-model="queryParams.车间"
          placeholder="请输入车间"
          clearable
          @keyup.enter.native="handleQuery"
          @change="handleSelectChange" >>
          <el-option
            v-for="item in option2"
            :key="item.value"
            :label="item.l"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="站点" prop="站点">
        <el-select
          v-model="queryParams.站点"
          placeholder="请输入站点"
          clearable
          @keyup.enter.native="handleQuery"
          @change="handleSelectChange" >
          <el-option
            v-for="item in option3"
            :key="item.value"
            :label="item.l"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="班次" prop="班次">
      <el-input
        v-model="queryParams.班次"
        placeholder="请输入班次"
        clearable
        @keyup.enter.native="handleQuery"
      />
      </el-form-item>
      <el-form-item label="日期" prop="日期">
      <el-date-picker
        v-model="queryParams.日期"
        type="date"
        placeholder="请选择日期"
        value-format="yyyy-MM-dd HH:mm"
        clearable
        @keyup.enter.native="handleQuery"
      />
    </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ECOS:ECOS:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ECOS:ECOS:edit']"
        >修改</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ECOS:ECOS:remove']"
        >删除</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="handleImport"
          v-hasPermi="['ECOS:ECOS:import']"
        >导入</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ECOS:ECOS:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="tableList"
      @selection-change="handleSelectionChange"
      border
      style="width: 100%;"
      :show-overflow-tooltip="true"
      empty-text="暂无数据"
    >
      <el-table-column type="selection" align="center" width="55"></el-table-column>
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="基地" align="center" prop="基地"/>
      <el-table-column label="车间" align="center" prop="车间"/>
      <el-table-column label="站点" align="center" prop="站点"/>
      <el-table-column label="一次合格率" align="center" prop="一次合格率"/>
      <el-table-column label="一次合格数" align="center" prop="一次合格数"/>
      <el-table-column label="总检数" align="center" prop="总检数"/>
      <el-table-column label="班次" align="center" prop="班次"/>
      <el-table-column label="日期" align="center" prop="日期"/>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
      <template slot-scope="scope">
      <el-button
        size="mini"
        type="text"
        icon="el-icon-edit"
        @click="handleUpdate(scope.row)"
        v-hasPermi="['system:user:edit']"
      >修改</el-button>
      <el-button
        size="mini"
        type="text"
        icon="el-icon-delete"
        @click="handleDelete(scope.row)"
        v-hasPermi="['system:user:remove']"
      >删除</el-button>
      </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="ID" prop="id">
              <el-input v-model="form.id" placeholder="请输入ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="基地" prop="基地">
              <el-select v-model="form.基地" placeholder="请输入基地" >
                <el-option
                  v-for="item in option1"
                  :key="item.value"
                  :label="item.l"
                  :value="item.value"
                  @change="handleSelectChange"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="车间" prop="车间">
              <el-select v-model="form.车间" placeholder="请输入车间" >
                <el-option
                  v-for="item in option2"
                  :key="item.value"
                  :label="item.l"
                  :value="item.value"
                  @change="handleSelectChange"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="站点" prop="站点">
              <el-select v-model="form.站点" placeholder="请输入站点" >
                <el-option
                  v-for="item in option3"
                  :key="item.value"
                  :label="item.l"
                  :value="item.value"
                  @change="handleSelectChange"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="合格数" prop="一次合格数">
              <el-input v-model="form.一次合格数" placeholder="请输入一次合格数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="总检数" prop="总检数">
              <el-input v-model="form.总检数" placeholder="请输入总检数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="班次" prop="班次">
              <el-input v-model="form.班次" placeholder="请输入班次"  />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="日期">
              <el-date-picker
                v-model="form.日期"
                type="date"
                placeholder="请选择日期"
                value-format="yyyy-MM-dd"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {listECOS, getid, insertecos, updateecos, deleteecos} from "@/api/ECOS/ECOS";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { getToken } from "@/utils/auth";

export default {
  name:"ECOS",
  dicts: ['sys_normal_disable'],
  data(){
    return{
      // 遮罩层
      loading: true,
      // 唯一标识符
      uniqueId: "",
      // 选中数组
      ids: [],
      // 菜单列表
      menuOptions: [],
      // 存储用户选择的数据项
      multipleSelection: [],
      // 选中表数组
      tableNames: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch:true,
      // 总条数
      total: 0,
      // 表数据
      tableList: [],
      // 日期范围
      dateRange: "",
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 弹出层标题
      title: "",
      // 表单参数
      form: {},

      defaultProps: {
        children: "children",
        label: "label"
      },

      // 用户导入参数
      upload:{
        // // 是否显示弹出层（用户导入）
        open: false,
        // // 弹出层标题（用户导入）
        title: "",
        // // 是否禁用上传
        isUploading: false,
        // // 是否更新已经存在的用户数据
        updateSupport: 0,
        // // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/ECOS/ECOS/importData"
      },

      // 查询参数
      queryParams:{
        pageNum: 1,
        pageSize: 10,
        ipaddr: undefined,
        name:undefined,
        tableName: undefined,
        tableComment: '' // 假设这是下拉列表绑定的模型
      },
      option1:[
        { value: '1', label: '1' },
        { value: '2', label: '2' },
        { value: '3', label: '3' },
        { value: '4', label: '4' },
        { value: '5', label: '5' },
      ],
      option2:[
        { label:'22-line', value: '22-line' },
        { label:'31-line', value: '31-line' },
        { label:'32-line', value: '32-line' },
        { label:'51-line', value: '51-line' },
        { label:'qd-line', value: 'qd-line' },
        { label:'dl-line', value: 'dl-line' },
        { label:'e-line' , value: 'e-line' },
        { label:'2-line' , value: '2-line' },
        { label:'fz-line', value: 'fz-line' },
      ],
      option3:[
        { label:'软件刷写' , value: '软件刷写' },
        { label:'车门检测' , value: '车门检测' },
        { label:'胎压检测' , value: '胎压检测' },
        { label:'整车配置' , value: '整车配置' },
        { label:'功能检测' , value: '功能检测' },
        { label:'四轮定位' , value: '四轮定位' },
        { label:'转毂检测' , value: '转毂检测' },
        { label:'终检报告' , value: '终检报告' },
      ],
      // // 列信息
      // columns: [
      //   { key: 0, label: `基地`, visible: true },
      //   { key: 1, label: `车间`, visible: true },
      //   { key: 2, label: `站点`, visible: true },
      //   { key: 3, label: `一次合格率`, visible: true },
      //   { key: 4, label: `一次合格数`, visible: true },
      //   { key: 5, label: `总检数`, visible: true },
      //   { key: 6, label: `日期`, visible: true },
      //   { key: 7, label: `班次`, visible: true }
      // ],
      // rules: {
      //   基地: [
      //     { required: true, message: "基地不能为空", trigger: "blur" }
      //   ],
      //   车间: [
      //     { required: true, message: "车间不能为空", trigger: "blur" },
      //   ],
      //   站点: [
      //     { required: true, message: "站点不能为空", trigger: "blur" },
      //   ],
      //   一次合格率: [
      //     { required: true, message: "一次合格率不能为空", trigger: "blur" },
      //   ],
      //   一次合格数: [
      //     { required: true, message: "一次合格数不能为空", trigger: "blur" },
      //   ],
      //   总检数: [
      //     { required: true, message: "总检数不能为空", trigger: "blur" },
      //   ],
      //   班次: [
      //     { required: true, message: "班次不能为空", trigger: "blur" },
      //   ],
      //   日期: [
      //     { required: true, message: "日期不能为空", trigger: "blur" },
      //   ]
      // }
    }
  },
  created() {
    this.getList();
  },
  activated() {
    const time = this.$route.query.t;
    if (time != null && time != this.uniqueId) {
      this.uniqueId = time;
      this.queryParams.pageNum = Number(this.$route.query.pageNum);
      this.getList();
    }
  },

  methods:{
    getList() {
      this.loading = true;
      listECOS(this.queryParams).then(response => {
        this.tableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        基地: undefined,
        车间: undefined,
        站点: undefined,
        一次合格率: undefined,
        一次合格数: undefined,
        总检数: undefined,
        日期: undefined,
        班次: undefined
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
      this.multipleSelection =selection;
    },

    handleSelectChange(newValue) {
      // 当选择的值变化时，这里将接收新的值作为参数
      console.log('选中的值变化为:', newValue);
      // 在这里可以执行进一步的逻辑处理
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getid(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改数据";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateecos(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            insertecos(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除ID为"' + ids + '"的数据项？').then(function() {
        return deleteecos(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },


    /** 导出按钮操作 */
    handleExport() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message:'请选择要导出的数据',
          type:'warning',
        });
        return ;
      }
      const ids=this.multipleSelection.map(item => item.id).join(',');
      this.download('ECOS/ECOS/export', {ids}, `user_${new Date().getTime()}.xlsx`)
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "数据导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('ECOS/ECOS/importTemplate', {
      }, `user_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
}
</script>
